<?php

/**
 * This is the model class for table "news".
 *
 * The followings are the available columns in table 'news':
 * @property string $id
 * @property string $news_title
 * @property string $news_content
 * @property string $author_name
 * @property integer $type_id
 * @property integer $status_id
 */
class News extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return News the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{news}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('news_title, news_content', 'required'),
			
			array('news_title', 'length', 'max'=>128, 'encoding'=>'utf-8'),
			array('news_content', 'length', 'max'=>8000, 'encoding'=>'utf-8'),
			array('author_name', 'length', 'max'=>10, 'encoding'=>'utf-8'),
			
			array('status_id, type_id', 'safe'),
			
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, news_title, news_content, author_name, type_id, status_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		    'status'=>array(self::BELONGS_TO, 'StatusType', 'status_id'),
		    'type'=>array(self::BELONGS_TO, 'NewsType', 'type_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'news_title' => '新闻标题',
			'news_content' => '新闻内容',
			'author_name' => '作者名称',
			'type_id' => '类型',
			'status_id' => '状态',
			'create_time' => '创建时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('news_title',$this->news_title,true);
		$criteria->compare('news_content',$this->news_content,true);
		$criteria->compare('author_name',$this->author_name,true);
		$criteria->compare('type_id',$this->type_id);
		$criteria->compare('status_id',$this->status_id);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * do some common field like create/update time and date
	 * @see CActiveRecord::beforeSave()
	 */
  public function beforeSave()
	{
	    if (parent::beforeSave()){
	    
	        if ($this->isNewRecord){
	            $this->create_time = $this->update_time = date('Y-m-d H:i:s');
	            $this->create_user_id = $this->update_user_id = Yii::app()->user->id;
	        }else {
	            $this->update_time = date('Y-m-d H:i:s');
	            $this->update_user_id = Yii::app()->user->id;
	        }
	        return true;
	    }else{
	        return false;
	    }
	}
	
	public function getLastNewsByType($type_id = 1)
	{
	    $type_id = (int)$type_id;	    
	    $results = News::model()->findAll('type_id = :type_id and status_id=1 order by create_time DESC limit 0, 7',array(':type_id'=>$type_id));
	    return $results;
	}

	/**
	 * 
	 * replace the give string with parten
	 * @param string $string
	 * @return string HTML format
	 */
    public function imgTagReplace($string)
    {
        $strNew = preg_replace('|<img .*?>|', '', $string);
        return $strNew;
    }
    
    /**
     * 
     * cut String
     * @param unknown_type $str
     * @param unknown_type $sublen
     * @param unknown_type $t
     */
    function mbSubstr($str, $sublen, $t=1)
    {
        if(strlen($str)<=$sublen) {
            $rStr = $str;
        } else {
            $I = 0;
            while ($I<$sublen) {
                $StringTMP = substr($str,$I,1);
                
                if (ord($StringTMP)>=224) {
                    $StringTMP = substr($str,$I,3);
                    $I = $I + 3;
                } elseif (ord($StringTMP)>=192) {
                    $StringTMP = substr($str,$I,2);
                    $I = $I + 2;
                } else {
                    $I = $I + 1;
                }
                
                $StringLast[] = $StringTMP;
            }
            
            $rStr = implode("",$StringLast);
            if ($t==1)
               $rStr .='...';    
        } 
        
        return $rStr;
    }
}